package com.igeek.shop.web.index;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.igeek.shop.entity.PageBeanTwo;
import com.igeek.shop.entity.Product;
import com.igeek.shop.service.shouye.IndexService;
import com.igeek.shop.service.shouye.IndexServiceImp;

public class KeysearchServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IndexService service = new IndexServiceImp();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		String state = req.getParameter("state") ;
		if(state.equals("search")){
			Keysearch(req,resp);
		}
	}

	private void Keysearch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//获取搜索关键字
		String search = req.getParameter("search");
		List<Product> list = service.search(search);
		//获取当前页
		String page= req.getParameter("page");
		int page1;
		if(page==null){
			page1=1;
		}else{
			page1 = Integer.parseInt(page);
		}
		long i = list.size();
		//进行数据分页操作
		PageBeanTwo pageBeantwo = service.getPageBeanByCategory1(page1,12,i,search);

		req.setAttribute("pageBeantwo", pageBeantwo);
		req.getRequestDispatcher("product_list2.jsp").forward(req,resp);
		
	}

	
}
